"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VPage = void 0;
var utils_1 = require("./utils");
var lifecycle_1 = require("./lifecycle");
var shared_1 = require("./shared");
var context_1 = require("./context");
var instance_1 = require("./instance");
var reactive_1 = require("./reactive");
/**
 * 页面入口方法
 * @param setup {PageSetup} setup方法
 */
function VPage(options) {
    var setupFn; // setup函数
    var miniOptions = {}; // 小程序Page原本的参数
    if (utils_1.isFunction(options)) {
        // options为函数，则options为setup函数
        setupFn = options;
    }
    else {
        if (options.setup === undefined) {
            // 如果options没有setup函数，则直接执行小程序Page方法
            return Page(options);
        }
        var setupFunc = options.setup, otherOptions = __rest(options, ["setup"]);
        setupFn = setupFunc;
        miniOptions = otherOptions;
    }
    // onLoad生命周期
    miniOptions[lifecycle_1.PageLifecycle.LOAD] = shared_1.wrapFuns(function (options) {
        instance_1.setCurrentInstance(this); // 保存Page实例，供给setup中的生命周期使用
        var context = context_1.createContext.call(this); // VPage创建实例，并绑定Page实例
        var SetupTask = setupFn.call(this, reactive_1.reactive(options), context); // 执行setup方法
        if (!utils_1.isPlainObject(SetupTask))
            console.error('setup返回格式错误');
        context.setData(SetupTask); // 将setup返回数据插入Page实例中
        instance_1.setCurrentInstance(null); // 清空储存的Page实例
    }, lifecycle_1.createLifecycleMethods(lifecycle_1.PageLifecycle.LOAD, miniOptions));
    var _loop_1 = function (key) {
        switch (lifecycle_1.PageLifecycle[key]) {
            case lifecycle_1.PageLifecycle.LOAD:
                break;
            case lifecycle_1.PageLifecycle.ADD_TO_FAVORITES:
            case lifecycle_1.PageLifecycle.SHARE_APP_MESSAGE:
            case lifecycle_1.PageLifecycle.SHARE_TIME_LINE:
                miniOptions[lifecycle_1.PageLifecycle[key]] = (function () {
                    var lifecycleFunc = lifecycle_1.createLifecycleMethods(lifecycle_1.PageLifecycle[key], miniOptions);
                    return function () {
                        var args = [];
                        for (var _i = 0; _i < arguments.length; _i++) {
                            args[_i] = arguments[_i];
                        }
                        var injectLifes = lifecycleFunc.apply(this, args);
                        return injectLifes[injectLifes.length - 1];
                    };
                })();
                break;
            default:
                miniOptions[lifecycle_1.PageLifecycle[key]] = lifecycle_1.createLifecycleMethods(lifecycle_1.PageLifecycle[key], miniOptions);
                break;
        }
    };
    // 其他生命周期
    for (var key in lifecycle_1.PageLifecycle) {
        _loop_1(key);
    }
    Page(miniOptions);
}
exports.VPage = VPage;
